package com.hjms.mall.services;

import cn.hutool.core.date.DateUtil;
import com.hjms.account.contracts.vo.account.AccountVo;
import com.hjms.account.share.IAccountFeignService;
import com.hjms.mall.contracts.ISkuService;
import com.hjms.mall.contracts.dto.sku.AddSkuDto;
import com.hjms.mall.contracts.vo.sku.SkuVo;
import com.hjms.mall.domain.Sku;
import com.hjms.mall.repository.SkuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.UUID;

/**
 * @author 何杨谊
 * @description: TODO
 * @date 2022/12/31 21:21
 */
@Service
public class SkuService implements ISkuService {
    @Resource
    private SkuMapper skuMapper;

    @Autowired
    private IAccountFeignService accountFeignService;

    @Override
    public String Add(AddSkuDto dto) {
        Sku entity=new Sku();
        entity.setId(UUID.randomUUID().toString());
        entity.setName(dto.getName());
        entity.setPrice(dto.getPrice());
        entity.setCreateAccount(UUID.randomUUID().toString());
        entity.setCreateTime(new Timestamp(DateUtil.current()));
        skuMapper.insert(entity);

        return  entity.getId();
    }

    @Override
    public SkuVo Get(String skuId) {
        // 从数据库获取商品信息
        Sku entity = skuMapper.selectById(skuId);

        // 组装返回商品信息
        SkuVo vo = new SkuVo();
        vo.setId(entity.getId());
        vo.setPrice(entity.getPrice());
        vo.setName(entity.getName());
        vo.setCreateAccount(entity.getCreateAccount());

        // 组装返回创建人信息
        // 远程服务调用账号微服务获取创建人基本信息
        AccountVo accountVo= accountFeignService.Get(entity.getCreateAccount());
        vo.setCreateAccountNickName(accountVo.getNickName());
        vo.setCreateAccountPhone(accountVo.getPhone());
        return vo;
    }
}
